//
//  PremiereViewController.m
//  myMusicApp
//
//  Created by 11_wyq on 15/6/14.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PremiereViewController.h"

@interface PremiereViewController ()
@property (retain ,nonatomic) MBProgressHUD *MBP;
@end

@implementation PremiereViewController

-(void)viewWillAppear:(BOOL)animated
{
   
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _headerPage = 1;
    _pageNumber = 0;

    //       给导航栏上放自定义label
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,WIDTH * 100,HEIGHT * 30)]autorelease];
    [view setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = view;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH * 30, 0,WIDTH * 50,HEIGHT * 30)];
    title.text = @"首页";
    title.textColor = [UIColor whiteColor];
    [title setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    [view addSubview:title];
    
    self.array=[NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    UIImage *image2 = [UIImage imageNamed:@"menu.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(locker)];
    //搜索
    UIImage *image3 = [UIImage imageNamed:@"Search@3x.png"];
    image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:image3 style:UIBarButtonItemStylePlain target:self action:@selector(searchBar)]autorelease];

    
    self.myTableView=[[[UITableView alloc]initWithFrame:CGRectMake(0,HEIGHT * 44, MYWIDTH,HEIGHT * 559) style:UITableViewStylePlain]autorelease];
    self.myTableView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    
    self.myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;//cell隐藏线
    
    self.myView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, MYWIDTH,HEIGHT * 44)]autorelease];
    self.myView.backgroundColor=[UIColor blackColor];
    [self.view addSubview:self.myView];
    

    
    //建Button 调方法
    [self makeButton];
    //建数组
    [self dictionaryAndNumber:1];
   
    //上面的滚动视图
    
    [self requestData];
    //默认显示"全部"的内容
    
//    //菊花
//    self.MBP = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    self.MBP.labelText = @"拼命加载中...";
//    [self.MBP show:YES];
    
    
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    
    self.isUpLoading = NO;
    [self addHeader:1];//下拉刷新
    [self addFooter:1 ];//上啦加载更多
    
      [self setAutomaticallyAdjustsScrollViewInsets:NO];
   
}


-(void)dictionaryAndNumber:(NSInteger)number
{
    
    _preDic = [NSMutableDictionary dictionary];//接口字典
    
    
    
    if (number == 1 ) {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"ALL" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
    } if (number==2)
    {
        
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"ML" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    } if (number==3)
    {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"HT" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    } if (number==4)
    {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"JP" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    } if (number==5)
    {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"KR" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    } if (number==6)
    {
        [_preDic setObject:@"0" forKey:@"D-A"];
        [_preDic setObject:@"US" forKey:@"area"];
        [_preDic setObject:@"0" forKey:@"offset"];
        [_preDic setObject:@"20" forKey:@"size"];
        
        
    }

    
}



//下拉刷新
-(void)addHeader:(NSInteger)number
{
    __block PremiereViewController *premierVC=self;
    

    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        premierVC.isUpLoading=NO;
        [self dictionaryAndNumber:number];//创建数组
        [premierVC requestData];
    }];
    
    //一进程序自动刷新
  
    //[self.myTableView.header beginRefreshing];
}



//上拉加载
-(void)addFooter:(NSInteger)number
{
    __block PremiereViewController *premierVC=self;
    self.myTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        premierVC.isUpLoading=YES;
        
       
        _pageNumber = _pageNumber + 20;
        NSString *newPage = [NSString stringWithFormat:@"%ld",(long)_pageNumber ];
        NSLog(@"newPage = %@",newPage);
        _preDic = [NSMutableDictionary dictionary];//接口字典
        
        [self dictionaryAndNumber:number];
        [_preDic setObject:newPage forKey:@"offset"];
        
        premierVC.nextPage ++;
        [premierVC requestData];
    }];
    
}



//建Button方法
-(void)makeButton
{
    

    
    _nameArray  = [NSMutableArray arrayWithObjects:@"全部",@"大陆",@"港台",@"日本",@"韩国",@"欧美", nil];
    
    for (int i = 0; i < 6; i++) {
        NSString *string = [_nameArray objectAtIndex:i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((10 + (i * 42 ) + (i * 20)) * WIDTH, 0, 50 *WIDTH, 40 * HEIGHT);
        [self.myView addSubview:button];
        button.tag = 1000 + i;
       // [button setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];//加粗字体
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:string forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
      [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor colorWithHexString:@"FF9966"] forState:UIControlStateNormal];

    
}



#pragma mark ---button点击触发

-(void)buttonAction:(UIButton *)button
{
    
    _pageNumber = 0;//下拉加载
    
    
    if (button == (UIButton *)[self.view viewWithTag:1000]) {
        
        self.array = [NSMutableArray array];
        [self dictionaryAndNumber:1];
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:1];//下拉刷新
        [self addFooter:1];//上啦加载更多
        
    }
    if (button == (UIButton *)[self.view viewWithTag:1001]) {
        
        
        self.array = [NSMutableArray array];
         [self dictionaryAndNumber:2];
        
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:2];//下拉刷新
        [self addFooter:2];//上啦加载更多
        
    }
    if (button == (UIButton *)[self.view viewWithTag:1002]) {
        
        
        self.array = [NSMutableArray array];
         [self dictionaryAndNumber:3];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:3];//下拉刷新
        [self addFooter:3];//上啦加载更多
    }
    if (button == (UIButton *)[self.view viewWithTag:1003]) {
        
        self.array = [NSMutableArray array];
         [self dictionaryAndNumber:4];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:4];//下拉刷新
        [self addFooter:4];//上啦加载更多
        
        
    }
    if (button == (UIButton *)[self.view viewWithTag:1004]) {
        
        self.array = [NSMutableArray array];
         [self dictionaryAndNumber:5];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1005] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:5];//下拉刷新
        [self addFooter:5];//上啦加载更多
    }
    if (button == (UIButton *)[self.view viewWithTag:1005]) {
        
        self.array = [NSMutableArray array];
       [self dictionaryAndNumber:6];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addHeader:6];//下拉刷新
        [self addFooter:6];//上啦加载更多
    }
    [button setTitleColor:[UIColor colorWithHexString:@"FF9966"] forState:UIControlStateNormal];

    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    
    
    [self requestData];


    
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count != 0) {
        return self.array.count;
    }else{
        return 0;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *myCell=@"cell";
    MVpremiereViewCell *cell=[tableView dequeueReusableCellWithIdentifier:myCell];
    if (cell == nil) {
        cell = [[[MVpremiereViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCell]autorelease];
        
        
    }
    
//    if (self.array.count != 0) {
//        cell.MVpreModel = [self.array objectAtIndex:indexPath.row];
//    }
    
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    //渐变效果
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.alpha = 1;
    [UIView commitAnimations];

    
    //收藏
    if (self.array.count != 0) {
        MVpreimereModel *mvPreimer = [self.array objectAtIndex:indexPath.row];
        cell.MVpreModel = [self.array objectAtIndex:indexPath.row];
        
        [[DataHandle shareHandleController] openDB];
        [[DataHandle shareHandleController] createCollectTable];
        
        NSMutableArray *array = [[DataHandle shareHandleController]selectCollect];
        if (array.count == 0) {
            cell.buer=NO;
        }else
        {
            for (CollectModel *collect in array) {
            NSString *modeID= [NSString stringWithFormat:@"%@",mvPreimer.idd];
                if ([collect.modelId isEqualToString:modeID]) {
                    cell.buer=YES;
                    cell.MVpreModel=[self.array objectAtIndex:indexPath.row];
                    return cell;
                }
            }
            cell.buer=NO;
            cell.MVpreModel=[self.array objectAtIndex:indexPath.row];
        }
    }
 
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return HEIGHT *200
    ;
    
}
//点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([DataHandle shareHandleController].monitorNetwork==NO) {
        
        UIAlertView *alertView =[ [UIAlertView alloc]initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        
    }else
    {
    
    VideoPlayerViewController *videoPlayer = [[[VideoPlayerViewController alloc]init]autorelease];
        
        if (self.array.count != 0) {
            self.premereModel=[self.array objectAtIndex:indexPath.row];
            videoPlayer.Id = self.premereModel.idd;
            videoPlayer.type =@"VIDEO";
            // NSLog(@"self.premereModel.type ==== %@",self.premereModel.type);
            
            videoPlayer.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:videoPlayer animated:YES];
        }
        
    
     }
    
    
}
//请求数据
-(void)requestData //nextPage:(NSInteger)nextPage
{
    //NSLog(@"_preDic = %@",_preDic);
    NSMutableDictionary *myDic = [NSMutableDictionary dictionaryWithDictionary:_preDic];
    
    
    

    
    [Connect ConnectRequestAFWithURL:FirstRun params:_preDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {
      
        
        NSMutableArray *resArray = [(NSMutableDictionary *)result objectForKey:@"videos"];
       
 
        //需要判断请没请求下来数据
        if (result == nil) {

           [GiFHUD dismiss];//动态图隐藏  
          //  [self.MBP hide:YES];//菊花隐藏

           
            [[DataHandle shareHandleController]openDB];


            [self.myTableView.header endRefreshing];//结束下拉刷新
            [self.myTableView.footer endRefreshing];// 结束上啦加载
          self.array =  [[DataHandle shareHandleController] selectPremiere];
            [self.myTableView reloadData];
            UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [aler show];
            [GiFHUD dismiss];//隐藏小黄人动态图
            [DataHandle shareHandleController].monitorNetwork=NO;
            
        }else{

           // self.array=[NSMutableArray array];
            [[DataHandle shareHandleController]openDB];
            [[DataHandle shareHandleController] dropPremiereTable];//删除表
            [[DataHandle shareHandleController] createPremiereTable];//创建表
        
        
        for (NSMutableDictionary *dic in resArray) {
            
            MVpreimereModel *MVpreModel = [[[MVpreimereModel alloc]init]autorelease];
            MVpreModel.idd= [dic objectForKey:@"id"];
            MVpreModel.title=[dic objectForKey:@"title"];
            MVpreModel.artistName = [dic objectForKey:@"artistName"];
            MVpreModel.albumImg = [dic objectForKey:@"albumImg"];
            MVpreModel.posterPic=[dic objectForKey:@"posterPic"];
            MVpreModel.type=@"VIDEO";
         
            if ([[myDic objectForKey:@"area"] isEqualToString:@"ALL"]) {
                
                [[DataHandle shareHandleController] insertPremiere:MVpreModel];//插入数据
            }
            
                [self.array addObject:MVpreModel];
            [DataHandle shareHandleController].monitorNetwork=YES;
        }
           //NSLog(@"array = %@",self.array);
//        [self.MBP hide:YES];//菊花隐藏
            [GiFHUD dismiss];//动态图隐藏
        [self.myTableView.header endRefreshing];//结束下拉

        [self.myTableView.footer endRefreshing];//结束上啦
        
        [self.myTableView reloadData];  
        
        }
    }];
}
//搜索
-(void)searchBar
{
    [GiFHUD dismiss];//隐藏小黄人动态图
    SearchViewController *search = [[[SearchViewController alloc]init]autorelease];
    search.hidesBottomBarWhenPushed = YES;//隐藏Bar
    
    [self.navigationController pushViewController:search animated:YES];
    
}




-(void)locker
{
    [GiFHUD dismiss];//动态图隐藏 
    [self.sideMenuViewController presentLeftMenuViewController];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
